from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings
from itsdangerous import SignatureExpired,BadData
import logging
logger = logging.getLogger('django')
def generic_openid_token(openid):
    #1.实例化对象
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=300)
    #2.组织数据
    data = {
        'openid':openid
    }
    #3.加密数据
    token = s.dumps(data)
    #4.返回数据
    return token.decode()
def check_openid_token(token):
    #1.实例化对象
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=300)
    #2.解密 解密的时候要捕获异常 怕有用户篡改加密后的openid
    try:
        result = s.loads(token)
    except BadData as e :
        logger.error(e)
        return None
    #3.获取解密后的数据
    return result.get('openid')
